package com.sys.service.impl;

import com.sys.dto.PageResult;
import com.sys.entity.BizTask;
import com.sys.mapper.BizTaskMapper;
import com.sys.service.TaskService;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    private BizTaskMapper bizTaskMapper;

    @Override
    public List<BizTask> findTasks(String domain, Integer difficulty, Integer age) {
        return bizTaskMapper.findTasks(domain, difficulty, age, null, null, null);
    }

    @Override
    public PageResult<BizTask> findTasksPage(String domain, Integer difficulty, Integer age, Integer pageNum, Integer pageSize) {
        // 设置默认值
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 20; // 默认每页20条
        }
        
        // 计算偏移量
        int offset = (pageNum - 1) * pageSize;
        
        // 查询总数
        long total = bizTaskMapper.countTasks(domain, difficulty, age);
        
        // 查询数据
        List<BizTask> records = bizTaskMapper.findTasks(domain, difficulty, age, pageNum, pageSize, offset);
        
        return new PageResult<>(records, total, pageNum, pageSize);
    }

    @Override
    public BizTask addCustomTask(BizTask task) {
        task.setIsPublic(0); // Mark as not a default task
        bizTaskMapper.save(task);
        return task;
    }

    @Override
    public BizTask modifyCustomTask(BizTask task) {
        task.setIsPublic(0);
        bizTaskMapper.save(task);
        return task;
    }

    @Override
    public void initTasksFromExcel() {
        try (InputStream is = getClass().getClassLoader().getResourceAsStream("tasks.xlsx")) {
            if (is == null) {
                throw new RuntimeException("tasks.xlsx not found in resources");
            }
            Workbook workbook = new XSSFWorkbook(is);
            Sheet sheet = workbook.getSheetAt(0);
            List<BizTask> tasks = new ArrayList<>();

            for (Row row : sheet) {
                if (row.getRowNum() == 0) { // Skip header row
                    continue;
                }
                BizTask task = new BizTask();
                // Assuming Excel columns order: domain, difficulty, age_min, age_max, title,
                // description, ...
                task.setDomain(row.getCell(0).getStringCellValue());
                task.setDifficulty((int) row.getCell(1).getNumericCellValue());
                task.setAgeMin((int) row.getCell(2).getNumericCellValue());
                task.setAgeMax((int) row.getCell(3).getNumericCellValue());
                task.setTitle(row.getCell(4).getStringCellValue());
                task.setDescription(row.getCell(5).getStringCellValue());
                task.setIsPublic(1); // Mark as public task
                task.setStatus(1); // Default status is active
                tasks.add(task);
            }
            if (!tasks.isEmpty()) {
                bizTaskMapper.saveBatch(tasks);
            }
        } catch (Exception e) {
            throw new RuntimeException("Failed to initialize tasks from Excel", e);
        }
    }
}